'use strict';

const Controller = require('egg').Controller;
// 发送支付功能
const AlipaySDK = require('alipay-sdk').default
// 将支付信息 formdata 包裹住
const AlipayFormData = require('alipay-sdk/lib/form').default

class PayController extends Controller {
    async alipay() {
        const { ctx, config } = this;
        const alipaySdk = new AlipaySDK({
            appId: '2021000118645837', // 你自己的沙箱环境的appId
            privateKey: "MIIEpAIBAAKCAQEApWB7O2vIu442gpZKyMVq1ptd2/zPLFc0Hp/M9HHnbuL6uWnVJiKaEy1B+4ghdK9cfMx/eireqBBJmzNff/89xkeyH2918vXwPpEuK0ldvJUFnM/a/gL0ffO8k1ne1b4MM0BaKTnfB5+AbVQHihCn5bNPTJs0kvZGEl7nA/rPH9FNzKKWCZjXIaXNxFB1KcmIhNfEYRBX8pYZg0XcLvUtjoXBAYqzA6coJaHquiVVuQN43CvyePpLmCCzeFwLnfTEe8AnYPbNRWjbsUvBfU3MzYEHKipPXEuX/f62bB7K7Tl3QouUCNFNC1n6PmD2rwzBO/CTVl5GlzCve+XdJgaaAQIDAQABAoIBACThMjobHEWvPjq692x0khHPzgucAjt6KvZVXqh4qkrRNWaZ0l3Rw5hZNGi6f/N8k9yE4jzFSKgkEbYZq8dojUclf/SAr8YB0JSzXZ9rmgrPFNRWihixYgKgk0Is/YOfASuAw1qEPFd/220GGzRP6JFR/nIv/nZzk0bPgScjDIOklhVzZgs5Q7FY6U2GcZvXVpe1R49x394rGPNcSYmFRHqhE8gCfCQ8WXR7cdyKhUx2MfH5BpQDkN/rHxp5XmiY4kD/NQ2ma9i4HmEdLfg0KYUz/oH8ZSd2OPA9T3E1VdUkXZq2vXupfWVQ6adEBTsTmUYpwzJxJ5YWurI6RbevIQ0CgYEA505tMcITevNKofcHFvE5ZX7uacp70WhJwuX5ZmtyMyS6gQEMJwgRUrxCjk5z2Gi5mFclwT2rV3ZSX1aimlzfqkCLyaEm3NVdCowPlzK5WIeA0VOAtOGYIYLScso7uvtC+wKmED5uwx4aJDT3UPIxZThlm/Yw6m6TV5z1q3k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UT1oiFQ2w1UKZMwV+OTEdD+I1gV5AOesGKSXPt9kU28/G6t0Eg/dLeIYXNQIDAQAB', // 支付宝公钥（不是应用公钥）
            gateway: 'https://openapi.alipaydev.com/gateway.do', // 网关地址
            timeout: 5000, // 网关超时时间
            camelcase: true // 是否把网关返回的下划线 key 转换为驼峰写法
        })

        const formData = new AlipayFormData();

        formData.setMethod('get'); //请求方式
        formData.addField('appId', '2021000118645837'); //用户id
        formData.addField('charset', 'utf-8'); //编码格式
        formData.addField('signType', 'RSA2');//注册信息格式
        formData.addField('bizContent', { //设置商户信息
            outTradeNo: new Date() * 1 + '', // 【必选】商户订单号：64个字符内，包含数字，字母，下划线；需要保证在商户端不重复
            productCode: 'FAST_INSTANT_TRADE_PAY', // 【必选】销售产品码(二维码)，目前仅支持FAST_INSTANT_TRADE_PAY
            totalAmount: '200', // 【必选】订单总金额，精确到小数点后两位
            subject: 'aaaaaaaaaaaaaaaaaa', // 【必选】 订单标题
            body: 'bbbbbbbbbbbbbbbbb' // 【可选】订单描述
        })
        console.log('formData:===============', formData)
        /**
         * exec对应参数：
         * method（调用支付宝api）
         * params（api请求的参数（包含“公共请求参数”和“业务参数”））
         * options（validateSign，formData，log）
         *
         * 统一下单支付 exec
         */
        ctx.body = await alipaySdk.exec('alipay.trade.page.pay', {}, { formData })
    }
}

module.exports = PayController;